package com.erp.controller;


import com.erp.entity.UserRole;
import com.erp.entity.result.Result;
import com.erp.entity.result.ResultStatus;
import com.erp.service.UserRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author pyj
 * @since 2020-11-07
 */
@RestController
@RequestMapping("/user-role")
public class UserRoleController {

    @Autowired
    private UserRoleService userRoleService;

    @GetMapping("/findUserRoleByName")
    public Result findUserRoleByName(String name) {
        return new Result(ResultStatus.SUCCESS, userRoleService.findUserRoleByName(name));
    }

    @GetMapping("/getUserRoleByPage")
    public Result getUserRoleByPage(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
                                     @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize) {

        PageHelper.startPage(page,pageSize);
        List<UserRole> list = userRoleService.getAll();
        PageInfo<UserRole> pageInfo = new PageInfo<>(list);
        return new Result(ResultStatus.SUCCESS, pageInfo);
    }


    @PostMapping("/add")
    public Result insert(@RequestBody UserRole userRole) {

        if (userRoleService.add(userRole) > 0) {
            return new Result(ResultStatus.SUCCESS, null);
        } else {
            return new Result(ResultStatus.BAD_REQUEST, null);
        }
    }

    @GetMapping("/delete")
    public Result delete(@PathParam("id") Integer id) {
        if (userRoleService.delete(id) > 0) {
            return new Result(ResultStatus.SUCCESS, null);
        } else {
            return new Result(ResultStatus.BAD_REQUEST, null);
        }
    }

    @PostMapping("/update")
    public Result update(@RequestBody UserRole userRole) {

        if (userRoleService.update(userRole) > 0) {
            return new Result(ResultStatus.SUCCESS, null);
        } else {
            return new Result(ResultStatus.BAD_REQUEST, null);
        }
    }
}

